//
//  YYETabBarViewController.m
//  YuYingEDU
//
//  Created by 胡波 on 2017/12/27.
//  Copyright © 2017年 胡波. All rights reserved.
//

#import "YYETabBarViewController.h"
#import "YYEHomePageViewController.h"
#import "YYECourseViewController.h"
#import "YYENotesViewController.h"
#import "YYEMineViewController.h"
#import "YYENaivigationController.h"
#import "UITabBar+badge.h"


@interface YYETabBarViewController ()

@end

@implementation YYETabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateSelected];
    
    [self setUpVC];
    
    // 我的问答
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getBadge) name:@"KAnswerNotification" object:nil];
    
    [self getBadge];
    
}

- (void)setUpVC {
    
    [self addchildVC:[[YYEHomePageViewController alloc]init] normalImage:@"homepage" selectedImage:@"homepage" VCTitle:@"首页"];
    
    [self addchildVC:[[YYECourseViewController alloc]init] normalImage:@"course" selectedImage:@"course" VCTitle:@"课程分类"];
    
    [self addchildVC:[[YYENotesViewController alloc]init] normalImage:@"notes" selectedImage:@"notes" VCTitle:@"心得笔记"];
    
    [self addchildVC:[[YYEMineViewController alloc]init] normalImage:@"mine" selectedImage:@"mine" VCTitle:@"我的"];
    
}

- (void)getBadge {
    
    
    NSDictionary *dic = @{
                          @"uid":[LoginModel shareLogin].uid,
                          @"username":[LoginModel shareLogin].username,
                          @"token":[LoginModel shareLogin].token,
                          };
    [SVProgressHUD show];
    [AFNHelper post:Badge parameters:dic success:^(id responseObject) {
        
        //        NSLog(@"%@",responseObject);
        if ([responseObject[@"status"] isEqual:@1]) {
            
            NSNumber *countInvite = [responseObject objectForKey:@"data"];

            if (countInvite.integerValue > 0) {
                
                [self.tabBar showBadgeOnItemIndex:3];
                
            }
            else {
                
                [self.tabBar hideBadgeOnItemIndex:3];
                
            }
            
        }
        [SVProgressHUD showWithStatus:responseObject[@"message"]];
        [SVProgressHUD dismiss];
        
    } faliure:^(id error) {
        
        NSLog(@"请求失败");
        [SVProgressHUD dismiss];
        
    }];
    
}

- (void)addchildVC:(UIViewController *)childVC normalImage:(NSString *)normalImage selectedImage:(NSString *)selectedImage VCTitle:(NSString *)vctitle {
    
    YYENaivigationController *navi = [[YYENaivigationController alloc]initWithRootViewController:childVC];
    
    childVC.tabBarItem.image = [[UIImage imageNamed:normalImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVC.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVC.tabBarItem.title = vctitle;
    [self addChildViewController:navi];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
